﻿using System.Collections.Generic;
using System.Diagnostics.Contracts;
using SharePointContentGenerator.ContentGeneratorLogic.ContentGenerators;
using SharePointContentGenerator.ContentGeneratorLogic.Core;
using SharePointContentGenerator.ContentGeneratorLogic.DataObjects;

namespace SharePointContentGenerator.ContentGeneratorLogic
{
    public class ContentGeneratorFacade
    {
        #region Events

        public event ContentGenerationItemEventHandler ContentGenerationStarted;

        private void DoContentGenerationStarted(ContentGeneratorItemEventArgs e)
        {
            if (ContentGenerationStarted != null)
            {
                ContentGenerationStarted(this, e);
            }
        }

        public event ContentGenerationItemEventHandler ContentGenerationFinished;

        private void DoContentGenerationFinished(ContentGeneratorItemEventArgs e)
        {
            if (ContentGenerationFinished != null)
            {
                ContentGenerationFinished(this, e);
            }
        }

        public event ContentGenerationItemEventHandler ContentGenerationChanging;

        private bool DoContentGenerationChanging(ContentGeneratorItemEventArgs e)
        {
            bool result = false;
            if (ContentGenerationChanging != null)
            {
                ContentGenerationChanging(this, e);
                result = e.Cancel;
            }

            return result;
        }

        #endregion Events

        #region Event Handlers

        internal void controller_ContentGenerationChanging(object sender, ContentGeneratorItemEventArgs e)
        {
            DoContentGenerationChanging(e);
        }

        internal void controller_ContentGenerationFinished(object sender, ContentGeneratorItemEventArgs e)
        {
            DoContentGenerationFinished(e);
        }

        internal void controller_ContentGenerationStarted(object sender, ContentGeneratorItemEventArgs e)
        {
            DoContentGenerationStarted(e);
        }

        #endregion Event Handlers

        #region Word Document Management

        public void CreateWordDocument(string documentPath)
        {
            WordDocumentGenerator controller = new WordDocumentGenerator();
            controller.CreateWordDocument(documentPath);
        }

        #endregion

        #region Base Library Management

        public List<string> LoadLibraries(string siteCollection, int templateId, string domain = "", string username = "", string password = "")
        {
            SharePointLibraryController controller = new SharePointLibraryController(siteCollection, domain, username, password);

            List<string> listNameList = controller.LoadLibraries(templateId);

            return listNameList;
        }

        public void DeleteLibraries(string siteCollection, List<string> librariesToDelete, string domain = "", string username = "", string password = "")
        {
            SharePointLibraryController controller = new SharePointLibraryController(siteCollection, domain, username, password);
            controller.ContentGenerationItemsStarted += controller_ContentGenerationStarted;
            controller.ContentGenerationItemsFinished += controller_ContentGenerationFinished;
            controller.ContentGenerationItemChanging += controller_ContentGenerationChanging;

            controller.DeleteLibraries(librariesToDelete);
        }

        #endregion Base Library Management

        #region Document Library Management

        /// <summary>
        /// This methods creates a number of lists in a given site collection.
        /// When no parameters are passed for authentication, the logged in user credentials are used.
        /// This method can also create documents within the document library.
        /// </summary>
        /// <param name="siteCollection">Url of the site collection, i.e. http://server/site, cannot be null or empty</param>
        /// <param name="baseLibraryName">The base name of the library to create, cannot be null or empty</param>
        /// <param name="numberOfLibraries">Number of libraries to create, must be strictly greater than 0</param>
        /// <param name="deleteIfExist">If true, it will delete the list if it already exist - optional parameter, defaults to false</param>
        /// <param name="addRandomDocuments">If true, it will add random Word documents in the list - optional parameter, defaults to false</param>
        /// <param name="minRandomDocument">Minimum random of documents to add - optional parameter, defaults to 10</param>
        /// <param name="maxRandomdocument">Maximum random of documents to add - optional parameter, defaults to 50</param>
        /// <param name="domain">Domain used for authenticaiton - optional parameter</param>
        /// <param name="username">Username used for authentication - optional parameter</param>
        /// <param name="password">Password used for authentication - optional parameter</param>
        /// <returns>A list of the names of the newly created document libraries</returns>
        public List<string> CreateDocumentLibraries(string siteCollection, string baseLibraryName = "", int numberOfLibraries = 1, bool deleteIfExist = false, bool addRandomDocuments = false, int minRandomDocument = 2, int maxRandomdocument = 5, string domain = "", string username = "", string password = "")
        {
            SharePointDocumentLibraryController controller = new SharePointDocumentLibraryController(siteCollection, domain, username, password);
            controller.ContentGenerationItemsStarted += controller_ContentGenerationStarted;
            controller.ContentGenerationItemsFinished += controller_ContentGenerationFinished;
            controller.ContentGenerationItemChanging += controller_ContentGenerationChanging;

            List<string> listNameList = controller.CreateDocumentLibraries(baseLibraryName, numberOfLibraries, deleteIfExist);
            if (addRandomDocuments)
                controller.PopulateLibrariesWithDocuments(listNameList, minRandomDocument, maxRandomdocument);

            return listNameList;
        }

        /// <summary>
        /// This methods creates a number of documents in the document libraries passed as parameter.
        /// When no parameters are passed for authentication, the logged in user credentials are used.
        /// </summary>
        /// <param name="siteCollection">Url of the site collection, i.e. http://server/site, cannot be null or empty</param>
        /// <param name="listNameList">A list of the names of the document libraries within which to create the documents</param>
        /// <param name="minRandomDocument">Minimum random of documents to add - optional parameter, defaults to 10</param>
        /// <param name="maxRandomdocument">Maximum random of documents to add - optional parameter, defaults to 50</param>
        /// <param name="domain">Domain used for authenticaiton - optional parameter</param>
        /// <param name="username">Username used for authentication - optional parameter</param>
        /// <param name="password">Password used for authentication - optional parameter</param>
        public void CreateDocumentsInDocumentLibraries(string siteCollection, List<string> listNameList, int minRandomDocument = 2, int maxRandomdocument = 5, string domain = "", string username = "", string password = "")
        {
            SharePointDocumentLibraryController controller = new SharePointDocumentLibraryController(siteCollection, domain, username, password);
            controller.PopulateLibrariesWithDocuments(listNameList, minRandomDocument, maxRandomdocument);
        }

        #endregion Document Library Management

        #region Picture Library Management

        /// <summary>
        /// This method returns a list of pictures based on random word search and number of pictures to download.
        /// This method uses the Flickr service.
        /// </summary>
        /// <param name="randomWords">A comma-seperated list of words to search on.</param>
        /// <param name="numPictures">The total number of images to download.</param>
        /// <returns>A List{<see cref="Picture"/>} based on the random word search</returns>
        /// <remarks>
        /// NOTE: This method is only temporary in order to test the download from Flickr. This will be encapsulated in a call to generate PictureLibraries with
        /// the number of images to download.
        /// </remarks>
        public List<Picture> GetRandomPictures(string randomWords, int numPictures)
        {
            PictureGenerator picGenerator = new PictureGenerator();
            picGenerator.ContentGenerationItemsStarted += controller_ContentGenerationStarted;
            picGenerator.ContentGenerationItemsFinished += controller_ContentGenerationFinished;
            picGenerator.ContentGenerationItemChanging += controller_ContentGenerationChanging;
            
            List<Picture> results = picGenerator.GetPhotosFromFlickr(randomWords, numPictures);

            return results;
        }

        /// <summary>
        /// This methods creates a number of Picture Libraries in a given site.
        /// When no parameters are passed for authentication, the logged in user credentials are used.
        /// This method can also add images to the Picture Libraries.
        /// </summary>
        /// <param name="site">Url of the site, i.e. http://server/site, cannot be null or empty</param>
        /// <param name="baseLibraryName">The base name of the library to create, cannot be null or empty</param>
        /// <param name="numberOfLibraries">Number of libraries to create, must be strictly greater than 0</param>
        /// <param name="deleteIfExist">If true, it will delete the list if it already exist - optional parameter, defaults to false</param>
        /// <param name="addRandomImages">If true, it will add random images to the library - optional parameter, defaults to false</param>
        /// <param name="minRandomImages">Minimum number of random images to add - optional parameter, defaults to 10</param>
        /// <param name="maxRandomImages">Maximum number of random images to add - optional parameter, defaults to 50</param>
        /// <param name="domain">Domain used for authenticaiton - optional parameter</param>
        /// <param name="username">Username used for authentication - optional parameter</param>
        /// <param name="password">Password used for authentication - optional parameter</param>
        /// <returns>A list of the names of the newly created Picture Libraries</returns>
        public List<string> CreatePictureLibraries(string site, string baseLibraryName = "", int numberOfLibraries = 1, bool deleteIfExist = false, bool addRandomImages = false, string randomWords = "", int minRandomImages = 2, int maxRandomImages = 5, string domain = "", string username = "", string password = "")
        {
            SharePointPictureLibraryController controller = new SharePointPictureLibraryController(site, domain, username, password);
            controller.ContentGenerationItemsStarted += controller_ContentGenerationStarted;
            controller.ContentGenerationItemsFinished += controller_ContentGenerationFinished;
            controller.ContentGenerationItemChanging += controller_ContentGenerationChanging;

            List<string> listNameList = controller.CreatePictureLibraries(baseLibraryName, numberOfLibraries, deleteIfExist, addRandomImages, randomWords, minRandomImages, maxRandomImages);
            //if (addRandomImages)
            //    controller.PopulateLibrariesWithImages(listNameList, randomWords, minRandomImages, maxRandomImages);

            return listNameList;
        }

        #endregion Picture Library Management
    }
}
